Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Custom Type support #959

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Louis-Tian
Copy link

This PR tries to solve two related issues in relation to custom types.

  1. Allow custom type to be added using type name instead of oid.
  2. Allow custom type to be added after initialisation.

Currently, to add a custom type one must know the oid of the type beforehand on initialisation.
This introduces a chicken and egg problem. In order to get the oid one must have a working instance to query the pg_type table to begin without. In addition, if a type is created on database during a session, there is no way to add a matching type on the client side without recreate a new Sql instance.

To solve this problem, this PR adds an addType function. For example

 const sql = postgres(options)
  await sql.addType('point', {
    serializer: (v) => `(${v.x},${v.y})`,
    parser: (v) => {
      const [x, y] = v.slice(1, -1).split(',')
      return { x: parseFloat(x), y: parseFloat(y) }
    }
  })
  await sql`create table test (p point)`
  const data = { x: 1, y: 2 }
  await sql`insert into test values(${sql.typed.point(data)})`

  const [{ p }] = await sql`select p from test`
  const equal = p.x === data.x && p.y === data.y

The idea of this PR is simple. we delay the registration of custom type after sql initialise. then use that connection to query the pg_type to get the oid and use it to add serialiser and parser dynamically.

One problem I am not sure how to solve is how merge the typescript definition of this new custom type into the existing TTypes. Just throw it out first to see what everyone think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant